<?php
class Konfigurasi extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('konfigurasi_model');
		$this->load->helper(array('form', 'url'));
		$this->load->library('upload');
	}
		
	function cek_validasi() {	
			// Setting Rules
			
			$this->form_validation->set_rules('text_keterangan', 'Keterangan Logo', 'required');
			$this->form_validation->set_rules('text_welcome', 'Teks Welcome', 'required');
		
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function input_konfigurasi()
    {
		$cek_konf = $this->konfigurasi_model->getId()->row();
		if ($this->cek_validasi() == FALSE){
			if($cek_konf->jml > 0){
				$data['status'] = 'edit';
				$data['rs_konfigurasi'] = $this->konfigurasi_model->get_konfigurasiID($cek_konf->ID_KONFIGURASI);
			}else
				$data['status'] = 'new';
			
			$data['content'] = $this->load->view('form_konfigurasi',$data,true);
			$this->load->view('main',$data);
		}
		else{
			$this->insert_konfigurasi();
		}            
    }
	
	function insert_konfigurasi(){
		$filenamees = explode('.',$_FILES['userfile']['name']);
		$filenamess = $filenamees[count($filenamees)-1];
		
		$data = array(
			'TEXT_KETERANGAN'	=> str_replace("[enter]", "<br>", $this->input->post('text_keterangan')),
			'TEXT_WELCOME'		=> $this->input->post('text_welcome')
		);
		$ins_konf = $this->konfigurasi_model->insert_konfigurasi($data);
		$last_id = $this->konfigurasi_model->getId()->row()->ID_KONFIGURASI;
		if($ins_konf == true){
			if($_FILES['userfile']['name']!= ''){
				$config['file_name'] = 'logo_cetak';
				$config['upload_path'] = './uploads/konfigurasi/';
				$config['allowed_types'] = 'png|jpg|gif|jpeg';
				$config['max_size']	= '1000';
				$this->upload->initialize($config);
				$this->load->library('upload', $config);
				
				if ( ! $this->upload->do_upload()){
					$data['err_msg'] = 'Maaf, Upload File Logo Gagal!';
					$data['status'] = 'edit';
					$data['rs_konfigurasi'] = $this->konfigurasi_model->get_konfigurasiID($last_id);
					$data['content'] = $this->load->view('form_konfigurasi',$data,true);
					$this->load->view('main',$data);
				}
				else {	
					$data2 = array();
					$data2['URL_LOGO_CETAK'] = 'logo_cetak.'.$filenamess;
					$this->konfigurasi_model->update_konfigurasi($data2,$getid);
					
					$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
					redirect('konfigurasi/input_konfigurasi');
					}				
			}else {	$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
					redirect('konfigurasi/input_konfigurasi');
				}
		}
		// $this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		// redirect('perpus/ms_konfigurasi'); 
	}
	
	function edit_konfigurasi($id_konf)
    {
 		if ($this->cek_validasi() == FALSE){
				$data['status'] = 'edit';
				$data['rs_konfigurasi'] = $this->konfigurasi_model->get_konfigurasiID($id_konf);
				$data['content'] = $this->load->view('form_konfigurasi',$data,true);
				$this->load->view('main',$data);
		}
		else{
			$this->update_konfigurasi($id_konf);
		}            
    }
	
	function update_konfigurasi($id_konf){
		$filenamees = explode('.',$_FILES['userfile']['name']);
		$filenamess = $filenamees[count($filenamees)-1];
		
		$data = array(
			'TEXT_KETERANGAN'	=> str_replace("[enter]", "<br>", $this->input->post('text_keterangan')),
			'TEXT_WELCOME'		=> $this->input->post('text_welcome')
		);
		$this->konfigurasi_model->update_konfigurasi($data,$id_konf);
		
		// if($ins_materi == true){
			if($_FILES['userfile']['name']!= ''){
				$config['file_name'] = 'logo_cetak';
				$config['upload_path'] = './uploads/konfigurasi/';
				$config['allowed_types'] = 'png|jpg|gif|jpeg';
				$config['max_size']	= '10000';
				$this->upload->initialize($config);
				$this->load->library('upload', $config);
				
				if($this->input->post('fileold')!= ''){
					unlink('uploads/konfigurasi/'.$this->input->post('fileold'));
				}
				
				if ( ! $this->upload->do_upload()){
					$data['err_msg'] = 'Maaf, Upload File Logo Gagal!';
					$data['status'] = 'edit';
					$data['rs_konfigurasi'] = $this->konfigurasi_model->get_konfigurasiID($id_konf);
					$data['content'] = $this->load->view('form_konfigurasi',$data,true);
					$this->load->view('main',$data);
				}
				else {	
						$data2 = array();
						$data2['URL_LOGO_CETAK'] = 'logo_cetak.'.$filenamess;
						$this->konfigurasi_model->update_konfigurasi($data2,$id_konf);
						
						$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
						redirect('konfigurasi/input_konfigurasi');
					}				
			}else {	$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
					redirect('konfigurasi/input_konfigurasi');
				}
		// }
	}
	
	function get_logo(){
		$url_logo = $this->konfigurasi_model->get_logo()->row()->FOTO;
		$header = $this->konfigurasi_model->get_header()->row()->TEXT_KETERANGAN;
		echo '<img src=" '.base_url().'images/'.$url_logo.'" width="36" height="36" border="0"></img>';
		echo '#'.$header;
	}
	
	//Delete
	function delete_surat()
	{
		$ta_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($ta_ids_post_array as $index => $ta_id){
			if (isset($ta_id) && $ta_id != "") {
				$this->materi_model->delete_surat($ta_id);
						
			
			$msg .= "Data upload file berhasil dihapus.\n";
			}
		}
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}
    
	function deletefile($id_surat,$namafile){
		//delete files //
		// var_dump($this->input->post('fileold'));exit;
		if($namafile!= ''){
			unlink('uploads/file/'.$namafile);
			
			$data = array(
				'URL_UPLOAD_SURAT'	=> ''
			);
			$this->materi_model->update_surat($data,$id_surat);
		
			$data['status'] = 'edit';
			// $data['matapelajaran'] = $this->pelajaran_model->get_mp();
			$data['rs_surat'] = $this->materi_model->get_suratID($id_surat);
			$data['content'] = $this->load->view('form_upload_surat',$data,true);
			$this->load->view('main',$data);
		}
	}
	
	function download_surat($id, $link){
		$data = array(
			'ID_SURAT'		=> $id,
			'JML_DOWNLOAD'	=> 1,
			'DOWNLOADER'	=> $this->session->userdata('userid'),
			'DOWNLOAD_TIME' => date('Y-m-d H:i:s')
		);
		$this->materi_model->insert_history_surat($data);
		redirect(base_url().'uploads/file/'.$link);
	}
}
?>
